﻿#region Copyright and Licensing Notice
// Copyright (C) 2011 Steven Bosnick
// 
// This program is open source software; you can redistribute it 
// and/or modifiy it under the terms of the Eclipse Public License,
// version 1.0.
// 
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A 
// PARTICULAR PURPOSE. See the Eclipse Public License for more
// details.
// 
// You should have received a copy of the Eclipse Public License 
// along with this program; if not, you can find a copy at
// "http://www.opensource.org/licenses/eclipse-1.0.php".
#endregion
using System;
using BitwiseOre.Properties;
using BitwiseOre.SharedMemory.Core.Common;

namespace BitwiseOre.SharedMemory.Core.Primatives
{
    [SharedFactory]
    internal sealed class GuidQueue : ISharedObjectInit, IGuidQueue
    {
        private static readonly string QueueEmptyMessage = Resources.GuidQueue_QueueEmptyMessage;
        private static readonly string QueueFullMessage = Resources.GuidQueue_QueueFullMessage;
        private const int PendingDequeuesIndex = 0;

        private readonly IIndexQueue _full;
        private readonly IIndexQueue _empty;
        private readonly IArray<Guid> _entries;
        private readonly IIntArray _pendingDequeues;

        public GuidQueue(IIndexQueue full, IIndexQueue empty, IArray<Guid> entries, IIntArray pendingDequeues)
        {
            _full = full;
            _empty = empty;
            _entries = entries;
            _pendingDequeues = pendingDequeues;
        }

        void ISharedObjectInit.InitializeNew()
        {
            for (var i = 0; i < _entries.Count; i++)
            {
                _empty.Enqueue(i); 
            }
        }

        void ISharedObjectInit.InitializeExisting()
        {
        }

        public void Enqueue(Guid guid)
        {
            var index = _empty.Dequeue();
            if (index == QueueConstants.NullIndex) throw new InvalidOperationException(QueueFullMessage);
            try
            {
                _entries[index] = guid;
                _full.Enqueue(index);
            }
            catch 
            {
                _empty.Enqueue(index);
                throw;
            }
        }

        public Guid Dequeue()
        {
            var index = _full.Dequeue();
            if (index == QueueConstants.NullIndex) throw new InvalidOperationException(QueueEmptyMessage);
            try
            {
                return _entries[index];
            }
            finally
            {
                _empty.Enqueue(index);
            }
        }

        public void IncrementPending()
        {
            while (true)
            {
                var oldvalue = _pendingDequeues[PendingDequeuesIndex];
                if (_pendingDequeues.InterlockedCompareExchange(PendingDequeuesIndex, oldvalue, oldvalue + 1))
                {
                    return;
                }
            }
        }

        public bool DecrementNonZeroPending()
        {
            while (true)
            {
                var oldvalue = _pendingDequeues[PendingDequeuesIndex];
                if (oldvalue == 0) return false;

                if (_pendingDequeues.InterlockedCompareExchange(PendingDequeuesIndex, oldvalue, oldvalue - 1))
                {
                    return true;
                }
            }
        }
    }
}